package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.constant.XFPayUrl;
import com.ocom.common.entity.company.EntityImportLog;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.SignatureUpdateRequest;
import com.ocom.common.request.web.CommonPidVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.PersonVo;
import com.ocom.common.vo.webapi.AddVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.feign.MinioClientService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.service.PersonnelPageService;
import com.ocom.webapi.vo.xfpersonnel.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class PersonnelPageController {
    @Autowired
    private PersonnelPageService personnelPageService;

    @Autowired
    private MinioClientService minioClientService;

    @Autowired
    CompanyClientService companyClientService;

    @Autowired
    HumanClientService humanClientService;


    @Autowired
    PayConfigClientService payConfigClientService;

    // 人员列表（PersonnelPage/onShow）
    @PostMapping(XFPayUrl.URL_PersonnelPageOnShow)
    public Result PersonnelPageOnShow(@Validated @RequestBody OnShowVo onShowVo){
        log.info("消费人员列表请求参数:{{}}",onShowVo);
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        onShowVo.setComId(yoCiUser.getComId());
        if(23==yoCiUser.getRole()){
            onShowVo.setDeptId(Long.valueOf(yoCiUser.getDeptId()));
        }
        return personnelPageService.PersonnelPageOnShow(onShowVo);
    }

    // 账号信息（PersonnelPage/info）
    @PostMapping(XFPayUrl.URL_PersonnelPageInfo)
    public Result PersonnelPageInfo(@Validated @RequestBody InfoVo infoVo){
        log.info("消费人员列表请求参数:{{}}",infoVo);
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        infoVo.setComId(yoCiUser.getComId());
        return personnelPageService.PersonnelPageInfo(infoVo);
    }

    // 查询人员未脱敏的信息（PersonnelPage/NoDesensitized）
    @GetMapping(XFPayUrl.URL_PersonnelPageNoDesensitized)
    public Result PersonnelPageNoDesensitized(Long comId,Long pId){
        log.info("查询人员未脱敏的信息请求参数:{{}},{{}}",comId,pId);
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        return personnelPageService.PersonnelPageNoDesensitized(yoCiUser.getComId(),pId);
    }

    // 新增人员信息（PersonnelPage/add）
    @PostMapping(XFPayUrl.URL_PersonnelPageAdd)
    public Result PersonnelPageAdd(@Validated @RequestBody AddVo addVo){
    //    if(true)  return ResultUtil.error(ResultEnum.Err_502);
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        addVo.setComId(yoCiUser.getComId());
        log.info("新增人员信息请求参数:{{}}",addVo);
        return personnelPageService.PersonnelPageAdd(addVo);
    }

    // 修改人员信息（PersonnelPage/modify）
    @PostMapping(XFPayUrl.URL_PersonnelPageModify)
    public Result PersonnelPageModify(@Validated @RequestBody ModifyVo modifyVo){
     //   if(true)  return ResultUtil.error(ResultEnum.Err_502);
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        modifyVo.setComId(yoCiUser.getComId());
        log.info("修改人员信息请求参数:{{}}",modifyVo);
        return personnelPageService.PersonnelPageModify(modifyVo);
    }

    // 删除人员信息（PersonnelPage/delete）
    @PostMapping(XFPayUrl.URL_PersonnelPageDelete)
    public Result PersonnelPageDelete(@Validated @RequestBody DeleteVo deleteVo){
    //    if(true)  return ResultUtil.error(ResultEnum.Err_502);
        log.info("删除人员信息请求参数:{{}}",deleteVo);
        if(!SecurityUtils.getYoCiUser().getComId().equals(deleteVo.getComId())){
            return ResultUtil.error(ResultEnum.Err_4100);
        }
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        deleteVo.setComId(yoCiUser.getComId());
        return personnelPageService.PersonnelPageDelete(deleteVo);
    }

    /**
     * 导入人员信息（PersonnelPage/import）
     * */
    @PostMapping(XFPayUrl.URL_PersonnelPageImport)

    public Result PersonnelPageImport( Long comId,Long accTypeId,@RequestParam(value = "importType",defaultValue = "1") Integer importType ,@RequestPart("file") MultipartFile file){
    //    if(true)  return ResultUtil.error(ResultEnum.Err_502);

        YoCiUser yoCiUser=SecurityUtils.getYoCiUser();
        comId = yoCiUser.getComId();

        Result result2 = minioClientService.upload(file);

        if(result2.getCode()==200){
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString( result2.getData()));

            EntityImportLog entityImportLog =new EntityImportLog();

            entityImportLog.setComId(comId);
            entityImportLog.setUrl(jsonObject.getString("imageUrl"));
            entityImportLog.setType(2);
            entityImportLog.setRemark("人员导入 admin:"+yoCiUser.getId() +"#acctype:"+accTypeId +"#importType:"+importType);

            companyClientService.importLog(entityImportLog);
        }


        log.info("导入人员信息请求参数:管理员【{}】单位【{}】身份类型【{}】importType【{}】file【{}】",yoCiUser.getId(),comId,accTypeId,importType,file);
        return  personnelPageService.PersonnelPageImport(comId,accTypeId,importType,file);
    }

    // 5.1.10批量办理人员离职（PersonnelPage/batchRemove）
    @PostMapping(XFPayUrl.URL_PersonnelPageBatchRemove)
    public Result PersonnelPageBatchRemove(@Validated @RequestBody BatchRemoveRequest request){
   //     if(true)  return ResultUtil.error(ResultEnum.Err_502);
        YoCiUser yoCiUser=SecurityUtils.getYoCiUser();
        request.setComId(yoCiUser.getComId());
        log.info("批量办理人员离职请求参数:{{}}",request);
        return personnelPageService.PersonnelPageBatchRemove(request);
    }


	//清空人员消费人脸
    @PostMapping(WebApiServiceUrl.Url_removerUserConsumePhoto)
    public Result removerUserConsumePhoto(@Validated @RequestBody CommonPidVo request){



        log.info("清空照片 oNlC5pft 【{}】",request);
        YoCiUser yoCiUser=SecurityUtils.getYoCiUser();
        Long comId = yoCiUser.getComId();


        if(yoCiUser.getRole()!=0l && yoCiUser.getRole()!=1l){
            return ResultUtil.error(ResultEnum.Err_4);
        }


        PersonVo personVo=new PersonVo();
        personVo.setComId(comId);
        personVo.setPId(request.getPId());
        personVo.setPPhotoUrl2("");

        Result result1 = humanClientService.modifyPhoto(personVo);
        if(result1.getCode()!=200){
            return ResultUtil.error(ResultEnum.Err_3014);
        }

        SignatureUpdateRequest signatureUpdateRequest=new SignatureUpdateRequest();
        signatureUpdateRequest.setComId(comId);
        signatureUpdateRequest.setPId(request.getPId());
        payConfigClientService.revokedSignRecords(signatureUpdateRequest);

        Result result24 = payConfigClientService.setXFWhiteInfo(comId,request.getPId(),0l);
        log.info("更新白名单返回 bT0jAU2d -result【{}】",result24);

        return ResultUtil.success();
    }
}
